package com.vote.action.question;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.vote.action.BaseAction;
import com.vote.entity.QuestionInfo;
import com.vote.entity.Respondent;
import com.vote.service.AnswerInfoManager;
import com.vote.service.PrizeInfoManager;
import com.vote.service.PrizeSettingManager;
import com.vote.service.QuestionInfoManager;
import com.vote.service.QuestionOptManager;
import com.vote.service.RespondentManager;
import com.vote.util.Struts2Utils;
import com.vote.util.VoteConstants;

public class LotteryAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3163973373816451566L;

	private static Logger log = LoggerFactory.getLogger(LotteryAction.class);
	
	@Autowired
	private QuestionInfoManager questionInfoManager;	
	@Autowired
	private QuestionOptManager questionOptManager;
	@Autowired
	private RespondentManager respondentManager;	
	@Autowired
	private AnswerInfoManager answerInfoManager;
	@Autowired
	private PrizeSettingManager prizeSettingManager;	
	@Autowired
	private PrizeInfoManager prizeInfoManager;
	
	private List<QuestionInfo> questionLst=null;
	//验证码
	private String kaptcha=null;
	private String message="";
	
	private String mobile="";	
	private String address="";	
	private String postCode="";
	private String email="";		
	private String userName="";
	
	public String lotteryInfo(){
		
		return SUCCESS;
	}	
	//抽奖
	public String startLottery(){	
		String ip=this.getRealIp();		
		String cookie=this.readCookie(VoteConstants.COOKIE_USER_KEY);
//		Map<String,String> prizeMap=prizeSettingManager.startLottery(ip, cookie);
		Map<String,String> prizeMap=null;
		Long startTime=System.currentTimeMillis();		
		prizeMap=prizeSettingManager.startLottery(ip, cookie);	
		
		Long endTime=System.currentTimeMillis();
		log.info("endTime-startTime:"+(endTime-startTime));
		Struts2Utils.renderJson(JSONObject.fromObject(prizeMap).toString());			
		return null;		
	}	
	//保存用户记录信息
	public String recordUserInfo(){
		message="";
		String ip=this.getRealIp();
		String cookie=this.readCookie(VoteConstants.COOKIE_USER_KEY);		
		Respondent resp= respondentManager.findRespondentByIpOrCookie(ip, cookie);
		if(resp!=null){
			resp.setAddress(address);
			resp.setMobile(mobile);
			resp.setEmail(email);
			resp.setUserName(userName);
			resp.setPostCode(postCode);
			prizeInfoManager.savePrizedUserInfo(resp);
		}else{
			message="信息异常！";
			return ERROR;
		}
		return SUCCESS;
	}	
	public QuestionInfoManager getQuestionInfoManager() {
		return questionInfoManager;
	}
	public void setQuestionInfoManager(QuestionInfoManager questionInfoManager) {
		this.questionInfoManager = questionInfoManager;
	}	
	public QuestionOptManager getQuestionOptManager() {
		return questionOptManager;
	}
	public void setQuestionOptManager(QuestionOptManager questionOptManager) {
		this.questionOptManager = questionOptManager;
	}

	public String getKaptcha() {
		return kaptcha;
	}
	public void setKaptcha(String kaptcha) {
		this.kaptcha = kaptcha;
	}
	public List<QuestionInfo> getQuestionLst() {
		return questionLst;
	}
	public void setQuestionLst(List<QuestionInfo> questionLst) {
		this.questionLst = questionLst;
	}
	public RespondentManager getRespondentManager() {
		return respondentManager;
	}
	public void setRespondentManager(RespondentManager respondentManager) {
		this.respondentManager = respondentManager;
	}
	public AnswerInfoManager getAnswerInfoManager() {
		return answerInfoManager;
	}
	public void setAnswerInfoManager(AnswerInfoManager answerInfoManager) {
		this.answerInfoManager = answerInfoManager;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public PrizeSettingManager getPrizeSettingManager() {
		return prizeSettingManager;
	}
	public void setPrizeSettingManager(PrizeSettingManager prizeSettingManager) {
		this.prizeSettingManager = prizeSettingManager;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPostCode() {
		return postCode;
	}
	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public PrizeInfoManager getPrizeInfoManager() {
		return prizeInfoManager;
	}
	public void setPrizeInfoManager(PrizeInfoManager prizeInfoManager) {
		this.prizeInfoManager = prizeInfoManager;
	}
	
	
	
}
